#region License, Terms and Conditions
//
// Prawks - Copyright (c) 2007 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
#endregion

namespace Prawks.DbMethods
{
    using System;
    using System.Data;

    using MethodImplAttribute = CodeProvisioning.MethodImplAttribute;

    /// <summary>
    /// When overridden in a derived class, specifies the command that is to
    /// be represented by an input (data access) method and binds the final
    /// implementation.
    /// </summary>

    [ AttributeUsage(AttributeTargets.Method) ]
    public abstract class DbMethodImplAttribute : MethodImplAttribute
    {
        private string _commandText;
        private CommandType _commandType;

        /// <summary>
        /// Initializes a new instance of the <see cref="DbMethodImplAttribute"/> 
        /// class for a stored procedure command.
        /// </summary>
        
        protected DbMethodImplAttribute() : 
            this(CommandType.StoredProcedure) {}

        /// <summary>
        /// Initializes a new instance of the <see cref="DbMethodImplAttribute"/> 
        /// for a specific command type.
        /// </summary>
        /// <param name="commandType">
        /// The type of the command to be created.
        /// </param>
        
        protected DbMethodImplAttribute(CommandType commandType) : 
            this(commandType, null) {}

        /// <summary>
        /// Initializes a new instance of the <see cref="DbMethodImplAttribute"/> 
        /// with a given command text and type.
        /// </summary>
        /// <param name="commandType">
        /// Indicates whether the command represents a Transact-SQL statement or
        /// a stored procedure name.
        /// </param>
        /// <param name="commandText">
        /// The Transact-SQL statement or the stored procedure name.
        /// </param>
        
        protected DbMethodImplAttribute(CommandType commandType, string commandText) 
        {
            if (commandType != CommandType.Text &&
                commandType != CommandType.StoredProcedure)
            {
                throw new ArgumentOutOfRangeException("commandType");
            }

            _commandText = commandText;
            _commandType = commandType;
        }

        /// <summary>
        /// Determines whether the <see cref="CommandText"/> is specified.
        /// </summary>
        
        public bool IsCommandTextDefined
        {
            get { return CommandText.Length != 0; }
        }

        /// <summary>
        /// Gets or sets the Transact-SQL statement or stored procedure to execute 
        /// at the data source.
        /// </summary>
        
        public string CommandText
        {
            get { return _commandText == null ? string.Empty : _commandText; }
            set { _commandText = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether <see cref="CommandText"/> 
        /// represent a Transact-SQL statement or a stored procedure. Only the
        /// <b>StoredProcedure</b> and <b>Table</b> values are valid.
        /// </summary>
        
        public CommandType CommandType
        {
            get { return _commandType; }
            
            set 
            { 
                if (value != CommandType.Text &&
                    value != CommandType.StoredProcedure)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                _commandType = value; 
            }
        }
    }
}
